package com.library.controller;

import com.library.entity.UserInfo;
import com.library.service.LoginService;
import com.library.service.UserInfoService;
import com.library.vo.BaseResult;
import com.library.vo.UserInfoRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class UserInfoController {

    @Autowired
    private LoginService loginService;

    public static final String prefix = "/api/userInfo";

    @Autowired
    public UserInfoService userInfoService;

    @RequestMapping("userinfo.html")
    public ModelAndView allBooks() {
        ArrayList<UserInfo> readers = new ArrayList<>();
        ModelAndView modelAndView = new ModelAndView("userinfo");
        modelAndView.addObject("readers", readers);
        return modelAndView;
    }

    @RequestMapping(path = prefix + "/list",
            method = RequestMethod.POST
    )
    @ResponseBody
    public BaseResult list(@RequestBody UserInfo userInfo) {
        List<UserInfoRes> list = this.userInfoService.list(userInfo);
        return BaseResult.successWithData(list);
    }

    @RequestMapping(path = prefix + "/add",
            method = RequestMethod.POST
    )
    @ResponseBody
    public BaseResult add(@RequestBody UserInfo userInfo ) {
        this.userInfoService.add(userInfo );
        return BaseResult.success();
    }

    @RequestMapping(path = prefix + "/delete",
            method = RequestMethod.POST
    )
    @ResponseBody
    public BaseResult delete(@RequestBody UserInfo userInfo) {
        this.userInfoService.delete(userInfo);
        return BaseResult.success();
    }

    @RequestMapping(path = prefix + "/update",
            method = RequestMethod.POST
    )
    @ResponseBody
    public BaseResult update(@RequestBody UserInfo userInfo) {
        this.userInfoService.update(userInfo);
        return BaseResult.success();
    }

    @RequestMapping(path = prefix + "/detail",
            method = RequestMethod.POST
    )
    @ResponseBody
    public BaseResult detail(@RequestBody UserInfo userInfo) {
        UserInfo userInfo1 = this.userInfoService.detail(userInfo);
        return BaseResult.successWithData(userInfo1);
    }


}
